<template>
    <view>
        <!-- pages/activity/activity.wxml -->
        <view class="container">
            <!-- 内容 -->
            <view class="class_content">
                <form @submit.stop.prevent="formSubmit" :report-submit="true" report-submit-timeout="500">
                    <block v-for="(item, index) in datalist" :key="item.afterclass">
                        <view class="cell" :id="index" @tap="activityClick">
                            <image class="banner" mode="aspectFill" :src="item.attachlist[0].attachment ? item.attachlist[0].attachment : minImgDoc + 'couse_bg.png'"></image>

                            <view class="tj_content">
                                <view class="title">
                                    {{ item.subject }}
                                    <text v-if="item.class && item.class != 'undefined'">{{ item.class }}</text>
                                </view>

                                <view class="info">
                                    <view class="info-content">
                                        <view class="time_content">
                                            <image class="small_icon" :src="minImgDoc + 'join_time.png'"></image>

                                            <view class="time">{{ item.starttime }} - {{ item.expiration }}</view>
                                        </view>

                                        <view class="count_content">
                                            <image class="small_icon" :src="minImgDoc + 'join_count.png'"></image>

                                            <view class="count">
                                                <text>已报 {{ item.applynumber }}</text>
                                                /{{ item.number == 0 ? '不限' : item.number }} 人
                                            </view>
                                        </view>
                                    </view>

                                    <view class="join" @tap.stop.prevent="joinActivity" :id="item.tid" hidden>报名</view>
                                </view>
                            </view>
                        </view>
                    </block>
                </form>
            </view>
            <view class="noMoreView" v-if="noMore">别再用力了，你已经滑到底啦</view>
        </view>

        <image :src="minImgDoc + 'course_post.png'" class="postEnter borderShowdow" @tap="postEnter">发布</image>

        <course_joinMask @joinSucceed="joinSucceed" :joinShow="joinShow" :tid="currenttid"></course_joinMask>
    </view>
</template>

<script>

// pages/activity/activity.js
const workListUrl = require('../../config').workListUrl;

const minImgDoc = require('../../config').minImgDoc;

const app = getApp();

export default {
    data() {
        return {
            minImgDoc: minImgDoc,
            datalist: [],
            loading: false,
            pagenum: 1,
            fid: 70,
            noMore: '',
            currenttid: '',
            joinShow: false
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.shareManage(options);
        this.makeRequest();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    onPullDownRefresh: function () {
        uni.showLoading({
            title: 'loading...',
            icon: 'loading'
        });
        this.requestMore(false);
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (!this.noMore) {
            this.requestMore(true);
        }
    },
    methods: {
        shareManage(options) {
            // sharetype = work & shareid=
            if (options.shareid) {
                uni.navigateTo({
                    url: '../activity_detail/activity_detail?tid=' + options.shareid
                });
            }
        },

        makeRequest() {
            this.loading= true;
            let getdata = {
                fid: this.fid,
                page: this.pagenum,
                mobile_api: 1
            };
            app.globalData.apimanager
                .getRequest(workListUrl, getdata)
                .then((res) => {
                    uni.stopPullDownRefresh();
                    uni.hideLoading();
                    let arr1 = res.Variables.forum_threadlist;

                    for (let i = 0; i < arr1.length; i++) {
                        var thread = arr1[i];

                        if (res.Variables.usernicknames[thread.authorid]) {
                            thread.nickname = res.Variables.usernicknames[thread.authorid];
                        } else {
                            thread.nickname = thread.author;
                        }
                    }

                    if (this.pagenum > 1) {
                        arr1 = this.datalist.concat(arr1);
                    }

                    var noMore = false;

                    if (arr1.length >= res.Variables.forum.threadcount) {
                        noMore = true;
                    }

                    this.setData({
                        loading: false,
                        datalist: arr1,
                        noMore: noMore
                    });
                })
                .catch((res) => {
                    uni.stopPullDownRefresh();
                    uni.hideLoading();
                    this.loading= false;
                    uni.showToast({
                        title: '出错了！',
                        icon: 'none'
                    });
                });
        },

        stopPullDownRefresh() {
            uni.stopPullDownRefresh({
                complete(res) {
                    uni.hideToast();
                }
            });
        },

        requestMore(isMore) {
            let pagenum = this.pagenum;

            if (isMore) {
                pagenum += 1;
            } else {
                pagenum = 1;
            }

            this.pagenum= pagenum // 更新当前页数;
            this.makeRequest(); // 重新调用请求获取下一页数据 或者刷新数据
        },

        joinActivity(e) {
            this.currenttid= e.currentTarget.id;this.joinShow= true;
        },

        activityClick(e) {
            const index = e.currentTarget.id;
            var item = this.datalist[index];
            uni.navigateTo({
                url: '../activity_detail/activity_detail?tid=' + item.tid + '&pid=' + item.pid
            });
        },

        postEnter() {
            if (app.globalData.member_identity == 2 && app.globalData.member_status != 1) {
                uni.showModal({
                    content: '老师身份正在审核，请耐心等待',
                    showCancel: false,
                    confirmText: '知道了'
                });
                return;
            }

            uni.navigateTo({
                url: '../activity_post/activity_post?fid=' + this.fid
            });
        },

        formSubmit() {
            console.log('占位：函数 formSubmit 未声明');
        },

        joinSucceed() {
            console.log('占位：函数 joinSucceed 未声明');
        }
    }
};
</script>
<style>
@import './activity.css';
</style>
